//
//  ZTodoMeetingDetailVC.m
//  WeClubs
//
//  Created by Zed Link on 10/4/2017.
//  Copyright © 2017 Zed. All rights reserved.
//

#import "ZTodoMeetingDetailVC.h"
#import "ZTodoMeetingView.h"
#import "ZCommentCell.h"
#import "ZClubMemberCell.h"
#import "ZCommentHeader.h"
#import "ZTodoMeetingParticipateVC.h"

static NSString * const MeetingCommentCellReuseID = @"ZMeetingCommentCellReuseID";
static NSString * const MeetingMemberCellReuseID = @"ZMeetingMemberCellReuseID";
static NSString * const CommentHeaderCellReuseID = @"ZCommentHeaderCellReuseID";

@interface ZTodoMeetingDetailVC () <UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *mainView;
@property (weak, nonatomic) IBOutlet ZTodoMeetingView *meetingHeaderView;

/** 签到负责人 */
@property (strong, nonatomic) NSArray *leaderModels;

/** 所有评论 */
@property (strong, nonatomic) NSArray *commentModels;

@end

@implementation ZTodoMeetingDetailVC

+ (instancetype)zNew
{
    return [self zStoryboardInstance];
}

- (void)zConfigNavigation
{
    [super zConfigNavigation];
    
    [self zAddBackItemWithGesture:YES];
    self.navigationItem.title = @"会议详情";
    self.navigationItem.rightBarButtonItem = [self zNavItemWithTitle:@"参与详情" andSelector:@selector(participateDetailAction)];
    
    [self zClearNavBgColorAndSeparator:NO];
    [self.navBgView setBackgroundColor:NAVBAR_BG_COLOR];
    [self.navSeparatorLine setImage:[UIImage imageNamed:@"shadow_nav"]];
}

- (void)participateDetailAction
{
    ZTodoMeetingParticipateVC *participateVC = [ZTodoMeetingParticipateVC zNew];
    participateVC.meetingModel = self.meetingModel;
    [self.navigationController pushViewController:participateVC animated:YES];
}

- (void)zLoadSubview
{
    [super zLoadSubview];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.mainView.rowHeight = UITableViewAutomaticDimension;
    self.mainView.estimatedRowHeight = 60;
    self.mainView.separatorColor = SEPARATOR_BG_COLOR;
    
    [self.mainView registerNib:[UINib nibWithNibName:@"ZClubMemberCell" bundle:nil] forCellReuseIdentifier:MeetingMemberCellReuseID];
    [self.mainView registerClass:[ZCommentHeader class] forHeaderFooterViewReuseIdentifier:CommentHeaderCellReuseID];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.meetingHeaderView.model = self.meetingModel;
    
    #warning Done
    NSDictionary *dataPara = @{
                               @"user_id" : @([ZUserInfo sharedInstance].userId),
                               @"token" : [ZUserInfo sharedInstance].token,
                               @"dynamic_type" :@"meeting",
                               @"dynamic_id" : @(self.meetingModel.meetingId),
                               };
    
    [HTTPRequest POST:kGet_dynamic_detailUrl parameter:dataPara success:^(id resposeObject) {
        
        NSArray *leaders = resposeObject[@"data"][@"leader"];
        
                 NSMutableArray *tempArrM = [NSMutableArray arrayWithCapacity:leaders.count];
                 for (NSDictionary *dict in leaders) {
                     ZClubMemberModel *model = [[ZClubMemberModel alloc] initWithDictionary:dict];
                     [tempArrM addObject:model];
                 }
                 self.leaderModels = tempArrM;
        
    } failure:^(NSError *error) {
        
    }];
    
//    [[ZNetManager sharedInstance] updateDetailOfTodoItem:2 withItemId:self.meetingModel.meetingId success:^(ZNetManagerStatus status, NSString *resultMsg, NSDictionary *data)
//     {
//         NSArray *leaders = data[@"leader"];
//         
//         NSMutableArray *tempArrM = [NSMutableArray arrayWithCapacity:leaders.count];
//         for (NSDictionary *dict in leaders) {
//             ZClubMemberModel *model = [[ZClubMemberModel alloc] initWithDictionary:dict];
//             [tempArrM addObject:model];
//         }
//         self.leaderModels = tempArrM;
//         
//     } failure:nil];
    
    
    
    #warning Done
    NSDictionary *dataParam = @{
                               @"user_id" : @([ZUserInfo sharedInstance].userId),
                               @"token" : [ZUserInfo sharedInstance].token,
                               @"source_type" : @"meeting",
                               @"source_id" : @(self.meetingModel.meetingId),
                               };
    
    [HTTPRequest POST:kGet_comment_listUrl parameter:dataParam success:^(id resposeObject) {
       
                 NSArray *comments = resposeObject[@"data"][@"comment"];
        
                 NSMutableArray *tempArrM = [NSMutableArray arrayWithCapacity:comments.count];
                 for (NSDictionary *dict in comments) {
                     ZCommentModel *model = [[ZCommentModel alloc] initWithDictionary:dict];
                     [tempArrM addObject:model];
                 }
                 self.commentModels = tempArrM;
    } failure:^(NSError *error) {
       
    }];
//    [[ZNetManager sharedInstance] updateCommentsOfTodoItem:2 withItemId:self.meetingModel.meetingId success:^(ZNetManagerStatus status, NSString *resultMsg, NSDictionary *data)
//     {
//         // Todo: 加载下一页数据
//         NSArray *comments = data[@"comment"];
//         
//         NSMutableArray *tempArrM = [NSMutableArray arrayWithCapacity:comments.count];
//         for (NSDictionary *dict in comments) {
//             ZCommentModel *model = [[ZCommentModel alloc] initWithDictionary:dict];
//             [tempArrM addObject:model];
//         }
//         self.commentModels = tempArrM;
//         
//     } failure:nil];
}

#pragma mark - UITableViewDelegate & UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (self.leaderModels.count == 0 || self.leaderModels == nil) {
        return 1;
    } else {
        return 2;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.leaderModels.count == 0 || self.leaderModels == nil) {
        return self.commentModels.count;
    } else {
        return section == 0 ? self.leaderModels.count : self.commentModels.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.leaderModels.count == 0 || self.leaderModels == nil || indexPath.section == 1) {
        ZCommentModel *commentModel = self.commentModels[indexPath.row];
        ZCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:MeetingCommentCellReuseID];
        cell.commentModel = commentModel;
        
        return cell;
    }
    else if (indexPath.section == 0) {
        ZClubMemberModel *memberModel = self.leaderModels[indexPath.row];
        ZClubMemberCell *cell = [tableView dequeueReusableCellWithIdentifier:MeetingMemberCellReuseID];
        cell.model = memberModel;
        
        return cell;
    }

    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    // 不实现该代理方法时, viewForHeaderInSection是从第1组开始, 而不是从0开始的
    return 36;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    NSDictionary *attr = @{
                           NSFontAttributeName : [UIFont systemFontOfSize:13 weight:UIFontWeightLight],
                           NSForegroundColorAttributeName : [UIColor zColorWithHex:0x666666],
                           };
    
    ZCommentHeader *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:CommentHeaderCellReuseID];
    header.style = ZTableHeaderStyleComment;
    header.alignment = ZTableHeaderAlignmentCenter;
    
    if (self.leaderModels.count == 0 || self.leaderModels == nil || section == 1) {
        NSString *count = [NSString stringWithFormat:@"共%zd条回复", self.commentModels.count];
        [header.descriptionBtn setAttributedTitle:[[NSAttributedString alloc] initWithString:count attributes:attr] forState:UIControlStateNormal];
        [header.descriptionBtn setImage:[UIImage imageNamed:@"ic_comment"] forState:UIControlStateNormal];
        
        return header;
    }
    else if (section == 0) {
        [header.descriptionBtn setAttributedTitle:[[NSAttributedString alloc] initWithString:@"签到负责人" attributes:attr] forState:UIControlStateNormal];
        [header.descriptionBtn setImage:[[UIImage alloc] init] forState:UIControlStateNormal];
        
        return header;
    }
    
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    // 没有Footer视图时, 该值可以体现为Group之间的间距
    return 12;
}

#pragma mark - Getter & Setter

- (void)setCommentModels:(NSArray *)commentModels
{
    _commentModels = commentModels;
    [self.mainView reloadData];
}

- (void)setLeaderModels:(NSArray *)leaderModels
{
    _leaderModels = leaderModels;
    [self.mainView reloadData];
}

@end
